/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import mint.mvc.annotation.BaseMapping;
import mint.mvc.annotation.Mapping;
import org.hibernate.Query;
import org.hibernate.Session;
import pojo.Feedback;
import pojo.User;
import util.HibernateUtil;
import util.JSONRenderer;

@BaseMapping("/feedback")
public class FeedbackAction {
    
    @Mapping(urls = {"/add"}, method = "post")
    public String logout(String pname, String email, String title, String words) {
        Session ss = HibernateUtil.getSessionFactory().openSession();
        ss.getTransaction().begin();
        Feedback cp = new Feedback();
        cp.setPname(pname);
        cp.setEmail(email);
        cp.setTitle(title);
        cp.setWords(words);
        
        ss.save(cp);
        ss.getTransaction().commit();
        ss.close();
        return "提交成功，返回<a href='http://120.76.84.119/'>首页</a>";
    }
    
    @Mapping(urls = {"/get"}, method = "get")
    public JSONRenderer get() {
       Session ss = HibernateUtil.getSessionFactory().openSession();
        String hql = "from Feedback";
        Query query = ss.createQuery(hql);
        List<User> list = query.list();
        ss.close();
        return new JSONRenderer(list);
    }
    
     @Mapping(urls = {"/del"}, method = "get")
    public JSONRenderer del(Integer country_id) {
        Session ss = HibernateUtil.getSessionFactory().openSession();
        ss.beginTransaction();

        Feedback iid = (Feedback) ss.get(Feedback.class, country_id);
        ss.delete(iid);
        
        ss.getTransaction().commit();
        ss.close();
        
        return new JSONRenderer(iid);
    }
}
